package lizhe.dummynode;

public class RotateRight {
    public static ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null)
            return head;

        int len = 0;
        ListNode tmp = head;
        while (tmp != null) {
            len++;
            tmp = tmp.next;
        }
        k %= len;


        ListNode tail = head;
        for (int i = 0; i < k; i++) {
            tail = findTail(head);
            tail.next = head;
            head = tail;
        }
        return tail;
    }

    private static ListNode findTail(ListNode head) {
        ListNode pre = null;
        while (head.next != null) {
            pre = head;
            head = head.next;
        }
        pre.next = null;

        return head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode list2 = new ListNode(2);
        ListNode list3 = new ListNode(3);
//        ListNode list4 = new ListNode(4);
//        ListNode list5 = new ListNode(5);
//        ListNode list6 = new ListNode(6);
//        ListNode list7 = new ListNode(7);
//        ListNode list8 = new ListNode(8);
//
        head.next = list2;
        list2.next = list3;
//        list3.next = list4;
//        list4.next = list5;
//        list5.next = list6;
//        list6.next = list7;
//        list7.next = list8;

        ListNode reverse = rotateRight(head, 2000000000);
        System.out.println("");


    }
}
